from dataclasses import dataclass
from pathlib import Path
from typing import NoReturn

from streamlit import config
from streamlit.web import bootstrap

from cfm.container import Container


@dataclass
class _StreamlitService:
    server_headless: bool = True
    server_port: int = 8501
    
    def start(self, target: str, args: list[str]) -> NoReturn:
        config.set_option("server.headless", self.server_headless)
        config.set_option("server.port", self.server_port)
        bootstrap.run(target, False, args, {})
        exit()
        
@dataclass
class FrontendService(_StreamlitService):
    workspaceName: str = ""
    workspaceDir: Path = Path()
    
    def start(self) -> NoReturn:
        Container.workspaceName = self.workspaceName
        Container.workspaceDir = self.workspaceDir
        
        super().start(
            "cfm/frontend/main.py",
            [
            ]
        )

@dataclass
class BackendService(_StreamlitService):
    workspaceName: str = ""
    workspaceDir: Path = Path()
    
    def start(self) -> NoReturn:
        Container.workspaceName = self.workspaceName
        Container.workspaceDir = self.workspaceDir
        
        super().start(
            "cfm/backend/main.py",
            [
            ]
        )